﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using FangChanCrmModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;

namespace FangChanCrm.Controllers
{
    public class AnChangController : Controller
    {

        private readonly It_AnChangBiaoService t_AnChangBiaoService;

        public AnChangController(It_AnChangBiaoService anChangBiaoService, IConfiguration configuration)
        {
            t_AnChangBiaoService = anChangBiaoService;
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public IActionResult AnChangAdd()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
        public IActionResult AnChangList()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
        public IActionResult AnChangUpdate()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        public string AnChangTianJia()
        {
            string anchangmingcheng = Request.Form["anchangmingcheng"];
            t_AnChangBiao anchang = new t_AnChangBiao();
            anchang.AnChangMingCheng = anchangmingcheng;
            anchang.ZhuangTai = "启用";
            int panduan = 0;
            panduan = t_AnChangBiaoService.Add(anchang);

            if (panduan > 0)
            {
                panduan = 1;
            }
            else
            {
                panduan = 0;
            }
            return panduan.ToString();
        }

        public string Getid()
        {
            string id = Request.Form["id"];
            t_AnChangBiao t_AnChangBiao = t_AnChangBiaoService.GetId(Convert.ToInt32(id));
            string shuju = "{\"anchangmingcheng\":\"" + t_AnChangBiao.AnChangMingCheng + "\"}";
            return shuju;
        }

        public string Update()
        {
            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var AnChang = _context.t_AnChangBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["id"]));
                AnChang.AnChangMingCheng = Request.Form["anchangmingcheng"].ToString();
                panduan = _context.SaveChanges() > 0;
            }
            if (panduan)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }

        public string QiYong()
        {
            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var AnChang = _context.t_AnChangBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["id"]));
                AnChang.ZhuangTai = "启用";
                panduan = _context.SaveChanges() > 0;
            }
            if (panduan)
            {
                return "success";
            }
            else
            {
                return "err";
            }
        }

        public string JinYong()
        {
            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var AnChang = _context.t_AnChangBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["id"]));
                AnChang.ZhuangTai = "启用";
                panduan = _context.SaveChanges() > 0;
            }
            if (panduan)
            {
                return "success";
            }
            else
            {
                return "err";
            }
        }

        public string InitializationDataTable()
        {
            t_AnChangBiao t_AnChangBiao = new t_AnChangBiao();
            return t_AnChangBiaoService.GetListWithFenYe(t_AnChangBiao, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
        }

    }
}
